package com.example.myfirstapplication

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.example.myfirstapplication.utils.BaseActivity
import com.example.myfirstapplication.utils.Tag
import kotlinx.android.synthetic.main.activity_radio_group.*

class RadioGroup : BaseActivity() {
    private var radioResult: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_radio_group)
        radiogroup1.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.radioButton1 -> {
                    radioResult = radioButton1.text.toString()
                }
                R.id.radioButton2 -> {
                    radioResult = radioButton2.text.toString()
                }
                R.id.radioButton3 -> {
                    radioResult = radioButton3.text.toString()
                }
                R.id.radioButton4 -> {
                    radioResult = radioButton4.text.toString()
                }
                R.id.radioButton5 -> {
                    radioResult = radioButton5.text.toString()
                }
            }
            Log.i(Tag, "选中了：$radioResult")
        }
        btn_radio.setOnClickListener {
            AlertDialog.Builder(this).apply {
                setTitle("提醒：")
                setMessage("你确定选择【$radioResult】吗?")
                setCancelable(false)
                setPositiveButton("确定") { dialog, which ->
                    val data = Bundle()
                    data.putString("radioVal", radioResult)
                    val intentObj = Intent(this@RadioGroup, SecondActivity::class.java)
                    intentObj.putExtras(data)
                    setResult(Activity.RESULT_OK, intentObj)
                    finish()
                }
                setNegativeButton("取消") { dialog, which ->
                    dialog.dismiss()
                }
                show()
            }
        }
    }
}